/*
 *  This file is part of "SchoolChoice",
 *  a school registration system.
 *
 *  Copyright (C) 2009 Nicolas Decerf, Olivier Jacquemin.
 *
 *  "SchoolChoice" is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  "SchoolChoice" is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program: see the file COPYING.txt.  If not, see
 *  http://www.gnu.org/licenses/.
 */

/*
 * SchoolsList class.
 */

/*global SchoolChoice */

(function() {
     // Private static data.

     // Shortcut notation.
     var WAC = SchoolChoice.WebAppClient;

     // Private static methods.
     function buildLi(item) {
         return "<li>" + item + "</li>";
     }

     // Class and constructor declaration.
     WAC.SchoolsList = function() { };

     // SchoolsList inherits from View.
     WAC.SchoolsList.prototype = new WAC.View();

     // Public instance properties and methods.

     WAC.SchoolsList.prototype._schools = null;

     WAC.SchoolsList.prototype._orderedSchoolsIds = [];

     WAC.SchoolsList.prototype.setSchools = function(schools) {
         var self = this;

         this._schools = schools;

         var selector = "#" + this._rootElementId;
         $(selector).empty();

         var school;
         var details;
         for (var i = 0; i < schools.length; i += 1) {
             school = schools[i];

             details = "<ul>";
             details += buildLi(school.streetAndNumber);
             details += buildLi(school.postalCode + " " + school.city);
             details += buildLi(school.availableSeats + " places disponibles");
             details += "<ul>";
             
             $(selector).append("<li><span>" + school.id + "</span>" + details + "</li>");
         }

         $(selector + " > li").click(function() {
             self._selectedSchoolId = this.firstChild.firstChild.data;
         });
     };

     WAC.SchoolsList.prototype.refreshOrderedSchoolsIds = function() {
         var self = this;
         self._orderedSchoolsIds = [];
         var selector = "#" + this._rootElementId + " > li > span";

         $(selector).each(function() {
             self._orderedSchoolsIds.push(this.firstChild.data);
         });
     };

     WAC.SchoolsList.prototype.getOrderedSchoolsIds = function() {
         return this._orderedSchoolsIds;
     };

     WAC.SchoolsList.prototype._getSchoolWithId = function(id) {
         var school;
         if (this._schools) {
             for (var i = 0; i < this._schools.length; i += 1) {
                 school = this._schools[i];
                 if (school.id === id) {
                     return school;
                 }
             }
         }
         return null;
     };

     WAC.SchoolsList.prototype.getSelectedSchool = function() {
         return this._getSchoolWithId(this._selectedSchoolId);
     };

     // Public static methods

 }) ();
